package com.drinks.yiniang.ui.fragment.community.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.user.wish.DrinksWishAdapter;
import com.drinks.yiniang.common.recycler.decoration.DrinksWishStaggeredItemDecoration;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.entity.user.wish.DrinksWishJson;
import com.drinks.yiniang.io.api.WishApiIO;
import com.drinks.yiniang.ui.activity.user.wine.WineListDetailActivity;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.drinks.yiniang.utils.ActivityUtils;
import com.hansen.library.ui.widget.refresh.MSwipeRefreshLayout;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.fragment.BaseLazyFragment;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;

/**
 * 他人用户社区-心愿单
 * Created by han on 2019/11/20
 * A simple {@link Fragment} subclass.
 */
public class UCWishListFragment extends BaseLazyFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener,
        BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.RequestLoadMoreListener {

    //refresh
    private MSwipeRefreshLayout refresh_uc_wish_list;
    private BaseRecyclerView rcv_uc_wish_list;

    private DrinksWishAdapter mDrinksWishAdapter;

    //查看的用户id
    private String mUserId;

    private int mPages;

    public UCWishListFragment() {
        // Required empty public constructor
    }

    public static UCWishListFragment newInstance(String userId) {

        Bundle bundle = new Bundle();
        bundle.putString(Constants.KeyUserId, userId);

        UCWishListFragment fragment = new UCWishListFragment();
        fragment.setArguments(bundle);

        return fragment;
    }


    @Override
    protected int getResID() {
        return R.layout.fragment_ucwish_list;
    }

    @Override
    protected void initView(View mView) {
        refresh_uc_wish_list = mView.findViewById(R.id.refresh_uc_wish_list);

        rcv_uc_wish_list = mView.findViewById(R.id.rcv_uc_wish_list);
    }

    @Override
    protected void initData() {
        mContext = getActivity();

        mUserId = getArguments() != null ? getArguments().getString(Constants.KeyUserId) : null;

        initAdapter();
    }

    private void initAdapter() {
        //2.声名为瀑布流的布局方式: 2列 ,垂直方向
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        //防止条目闪动
        staggeredGridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);

        rcv_uc_wish_list.setLayoutManager(staggeredGridLayoutManager);
        rcv_uc_wish_list.addItemDecoration(new DrinksWishStaggeredItemDecoration(mContext, 10));

        mDrinksWishAdapter = new DrinksWishAdapter(mContext, new ArrayList<DrinksWishJson.DrinksWishList>());
        mDrinksWishAdapter.setCommonEmpty(getLayoutInflater(), rcv_uc_wish_list);
        mDrinksWishAdapter.bindToRecyclerView(rcv_uc_wish_list);
    }

    @Override
    protected void listener() {
        refresh_uc_wish_list.setOnRefreshListener(this);
        mDrinksWishAdapter.setOnItemClickListener(this);
        mDrinksWishAdapter.setOnItemChildClickListener(this);
        mDrinksWishAdapter.setOnLoadMoreListener(this, rcv_uc_wish_list);
    }

    @Override
    protected void widgetClick(View v) {

    }

    @Override
    protected void lazyLoadData() {
        getWishList();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        DrinksWishJson.DrinksWishList data = mDrinksWishAdapter.getItem(position);

        if (data == null) {
            return;
        }

        Intent intent = new Intent(mContext, WineListDetailActivity.class);
        intent.putExtra(Constants.KeyId, data.getId());
        startActivity(intent);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if(!ActivityUtils.isLogined(this, mContext, true)) {
            return;
        }

        DrinksWishJson.DrinksWishList data = mDrinksWishAdapter.getItem(position);

        if (data == null) {
            return;
        }

        switch (view.getId()) {
            case R.id.tv_drinks_wish_follows_num://点赞
                doLike(data, position);
                break;
            default:
                break;
        }
    }

    @Override
    public void onRefresh() {
        getWishList();
    }

    @Override
    public void onLoadMoreRequested() {
        WishApiIO.getInstance().getOtherWishWineList(mUserId, mPages, new APIRequestCallback<DrinksWishJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(DrinksWishJson res) {
                if (mDrinksWishAdapter == null) {
                    return;
                }

                mPages++;

                mDrinksWishAdapter.addData(res.getData().getData());

                if (CommonUtils.isHasMoreData(res.getData().getData())) {
                    mDrinksWishAdapter.loadMoreComplete();
                } else {
                    mDrinksWishAdapter.loadMoreEnd();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mDrinksWishAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    //获取心愿单列表
    private void getWishList() {
        if (StringUtils.isEmpty(mUserId)) {
            refresh_uc_wish_list.setRefreshing(false);
            ToastUtils.showShort(R.string.text_user_not_exist);
            return;
        }

        if (!refresh_uc_wish_list.isRefreshing()) {
            showLoadingDialog();
        }

        mPages = 1;

        WishApiIO.getInstance().getOtherWishWineList(mUserId, mPages, new APIRequestCallback<DrinksWishJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(DrinksWishJson res) {
                if (mDrinksWishAdapter == null) {
                    return;
                }

                mPages++;

                mDrinksWishAdapter.getData().clear();
                mDrinksWishAdapter.addData(res.getData().getData());

                if (CommonUtils.isHasMoreData(res.getData().getData())) {
                    mDrinksWishAdapter.loadMoreComplete();
                } else {
                    mDrinksWishAdapter.loadMoreEnd();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mDrinksWishAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
                refresh_uc_wish_list.setRefreshing(false);
                dismissDialog();
            }
        });
    }

    //点赞
    private void doLike(final DrinksWishJson.DrinksWishList data, final int pos) {
        if (StringUtils.isEmpty(data.getId())) {
            ToastUtils.showShort(R.string.text_community_not_exist);
            return;
        }

        showLoadingDialog();

        WishApiIO.getInstance().doWishListLike(data.getId(), new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                if (CommonUtils.isArrayIndexOutOfBounds(mDrinksWishAdapter.getData(), pos)) {
                    return;
                }

                data.setLike_count(res.getData());
                data.setIs_like(Constants.STATUS_GLOBAL_YES.equals(data.getIs_like()) ? Constants.STATUS_GLOBAL_NO : Constants.STATUS_GLOBAL_YES);

                mDrinksWishAdapter.setData(pos, data);
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
